package com.hh.vietnameseproject.logic.network

import com.hh.vietnameseproject.logic.interceptor.HeaderInterceptor
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.http.Field
import java.lang.RuntimeException
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine


object AllNetWork {
    private val loginService = ServiceCreator.create(LoginService::class.java)
    private val mainService = ServiceCreator.create(MainService::class.java)

    //获取手机验证码
    suspend fun getPhoneCode(phone: String, checkcode: String) =
        loginService.getPhoneCode(phone, checkcode).await()

    suspend fun getCheckCode() = loginService.getCheckCode().await()

    suspend fun login(phone: String, phonecode: String) =
        loginService.login(phone, phonecode).await()

    suspend fun loginPwd(phone: String, pwd: String, checkcode: String) =
        loginService.loginPwd(phone, pwd, checkcode).await()

    suspend fun getRegistPhoneCode(phone: String, checkcode: String) =
        loginService.getRegistPhoneCode(phone, checkcode).await()

    suspend fun registPhone(phone: String, phonecode: String, name: String) =
        loginService.registPhone(phone, phonecode, name).await()

    suspend fun forgetCode(phone: String, phonecode: String, pwd: String) =
        loginService.forgetCode(phone, phonecode, pwd).await()

    suspend fun addPassword(password: String) = loginService.addPassword(password).await()

    suspend fun changePassword(old: String, new: String) =
        loginService.changePassword(old, new).await()

    suspend fun getHeadImage(userId: String) = mainService.getHeadImage(userId).await()

    suspend fun changeName(name: String) = mainService.changeName(name).await()

    suspend fun clickCard() = mainService.clickCard().await()

    suspend fun translate(type: String, text: String) = mainService.translate(type, text).await()

    suspend fun getSchedule() = mainService.getSchedule().await()

    suspend fun gettThesaurus() = mainService.gettThesaurus().await()

    suspend fun getWords(id:String)= mainService.getWords(id).await()

    suspend fun getWebsite() = mainService.getWebsite().await()

    suspend fun getRecord() = mainService.getRecord().await()


    suspend fun updateSchedule(
        thesaurusId: String,
        plandays: String,
        nowlearning: String,
        daylearncount: String
    ) = mainService.updateSchedule(thesaurusId, plandays, nowlearning, daylearncount).await()

    private suspend fun <T> Call<T>.await(): T {
        return suspendCoroutine {
            enqueue(object : Callback<T> {
                override fun onFailure(call: Call<T>, t: Throwable) {
                    it.resumeWithException(t)
                }

                override fun onResponse(call: Call<T>, response: Response<T>) {
                    val body = response.body()
                    if (HeaderInterceptor.cookies == null) {
                        HeaderInterceptor.cookies = response.headers().get("set-cookie")
                    }

                    if (body != null) it.resume(body)
                    else it.resumeWithException(RuntimeException("the body is null"))
                }
            })

        }
    }
}